package com.bnyy.medicalHousekeeper.moudle.wallet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.bnyy.common.view.FormInfoItem;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.wallet.bean.BankCardInfo;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class AddBankCardActivity extends BaseActivityImpl {
    @BindView(R.id.form_name)
    FormInfoItem formName;
    @BindView(R.id.form_bank_card_num)
    FormInfoItem formBankCardNum;
    @BindView(R.id.form_bank)
    FormInfoItem formBank;
    @BindView(R.id.form_id_card_num)
    FormInfoItem formIdCardNum;
    @BindView(R.id.form_phone)
    FormInfoItem formPhone;

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_bank_card;
    }

    @Override
    public String getTitleStr() {
        return "添加银行卡";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        formBankCardNum.setInputType(InputType.TYPE_CLASS_NUMBER);
//        formPhone.setInputType(InputType.TYPE_CLASS_NUMBER);
        formPhone.setEtContentMaxLength(11);
    }

    @OnClick(R.id.tv_confirm)
    public void onViewClicked() {
        String name = formName.getContent();
        String bankCardNum = formBankCardNum.getContent();
        String bank = formBank.getContent();
        String idCardNum = formIdCardNum.getContent();
        String phone = formPhone.getContent();

        if (TextUtils.isEmpty(name)) {
            Toast.makeText(mContext, formName.getHint() + formName.getTitle(), Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(bankCardNum)) {
            Toast.makeText(mContext, formBankCardNum.getHint(), Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(bank)) {
            Toast.makeText(mContext, formBank.getHint(), Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(idCardNum)) {
            Toast.makeText(mContext, formIdCardNum.getHint(), Toast.LENGTH_SHORT).show();
            return;
        }

        if (!idCardNum.matches("^[xX0-9]+$") || idCardNum.length() != 18) {
            Toast.makeText(mContext, "请输入正确的身份证号", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(mContext, formPhone.getHint(), Toast.LENGTH_SHORT).show();
            return;
        }

        if (phone.length() != 11) {
            Toast.makeText(mContext, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
            return;
        }

        if (bankCardNum.length() < 16) {
            Toast.makeText(mContext, "请输入正确的银行卡号", Toast.LENGTH_SHORT).show();
            return;
        }

        BankCardInfo bankCardInfo = new BankCardInfo();
        bankCardInfo.setUsername(name);
        bankCardInfo.setBank_card_no(bankCardNum);
        bankCardInfo.setIdentity(idCardNum);
        bankCardInfo.setBank_card_phone(phone);
        bankCardInfo.setBank_name(bank);
        bankCardInfo.setOpening_bank(bank);

        requestManager.request(requestManager.mJavaRetrofitService.addBankCard(RequestManager.getJsonRequestBody(bankCardInfo)),
                new BaseObserverImpl<Object>() {

                    @Override
                    public void onSuccess(Object o) {
                        super.onSuccess(o);
                        if (o instanceof Map) {
                            Object id = ((Map) o).get("id");
                            if (id instanceof Double) {
                                bankCardInfo.setId(((Double) id).intValue());
                            }

                            if (id instanceof Integer) {
                                bankCardInfo.setId((Integer) id);
                            }

                            if (bankCardInfo.getId() == 0) {
                                Toast.makeText(mContext, "添加银行卡失败", Toast.LENGTH_SHORT).show();
                            } else {
                                Intent intent = new Intent();
                                intent.putExtra("bankCardInfo", bankCardInfo);
                                setResult(RESULT_OK, intent);
                                finish();
                            }
                        }
                    }
                });
    }
}



































